package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.domain.dao.CallCenterCallLogDao;
import com.xbongbong.saas.domain.entity.CallCenterCallLogEntity;
import com.xbongbong.saas.enums.CallLogResultEnum;
import com.xbongbong.saas.model.CallCenterCallLogModel;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author youli.chen
 * @version v1.0
 * @description: 呼叫中心通话记录服务层实现类
 * @date 2019/2/18 21:30
 * @since v1.0
 */
@Service("callCenterCallLogModel")
public class CallCenterCallLogModelImpl implements CallCenterCallLogModel {

    @Resource
    private CallCenterCallLogDao callCenterCallLogDao;
    @Resource
    private PaasEsModel paasEsModel;

    /**
     * 批量插入通话记录
     *
     * @param list 通话记录列表
     */
    @Override
    public void batchInsert(List<CallCenterCallLogEntity> list) {
        callCenterCallLogDao.batchInsert(list);
    }

    /**
     * 根据主键和公司id进行删除
     *
     * @param key    主键
     * @param corpid 公司id
     * @return Integer MyBatis操作结果 1：成功
     */
    @Override
    public Integer deleteByKey(Long key, String corpid) {
        return callCenterCallLogDao.deleteByKey(key, corpid);
    }

    /**
     * 根据主键和公司id进行精准查找
     *
     * @param key    主键
     * @param corpid 公司id
     * @return CallCenterCallLogEntity 话务中心通话记录信息
     */
    @Override
    public CallCenterCallLogEntity getByKey(Long key, String corpid) {
        return callCenterCallLogDao.getByKey(key, corpid);
    }

    /**
     * 根据uuid获取通话记录
     *
     * @param corpid   公司id
     * @param callUuid
     * @return
     */
    @Override
    public CallCenterCallLogEntity getDetailById(String corpid, String callUuid) {
        return callCenterCallLogDao.getDetailById(corpid, callUuid);
    }

    /**
     * 计算该公司的通话总时长
     *
     * @param corpid    公司id
     * @param startTime 计算的开始时间
     * @param endTime   计算的结束时间
     * @return 通话时长
     * @author youli.chen
     * 修改时间：
     * @version v1.0
     * @since v1.0
     */
    @Override
    public Integer sumCallDuration(String corpid, Integer startTime, Integer endTime) {
        Map<String, Object> params = new HashMap<>(16);
        params.put("corpid", corpid);
        params.put("result", CallLogResultEnum.SUCCESS.getCode());
        params.put("startCallTime", startTime);
        params.put("endCallTime", endTime);
        return callCenterCallLogDao.sumCallDuration(params);
    }

    /**
     * 插入
     * ES这边处理通话记录，特殊化处理，不遵循表单的拓展处理
     *
     * @param entity 实体
     * @return 是否成功
     * @throws XbbException 数据库操作异常
     */
    @Override
    public Integer insert(CallCenterCallLogEntity entity) throws XbbException {
        int now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        Integer n = callCenterCallLogDao.insert(entity);

        InsertDTO insertDTO = new InsertDTO();
        insertDTO.setEsId(entity.getCorpid() + "_" + entity.getId());
        insertDTO.setSource(BeanUtil.convertBean2Map(entity, false, true));
        insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_CALL_CENTER_CALL_LOG);
        paasEsModel.insert(insertDTO);
        return n;
    }

    /**
     * 更新
     * ES这边处理通话记录，特殊化处理，不遵循表单的拓展处理
     *
     * @param entity 实体
     * @return 是否成功
     * @throws XbbException 数据库操作异常
     */
    @Override
    public Integer update(CallCenterCallLogEntity entity) throws XbbException {
        int now = DateUtil.getInt();
        entity.setUpdateTime(now);
        Integer id = callCenterCallLogDao.update(entity);

        InsertDTO insertDTO = new InsertDTO();
        insertDTO.setEsId(entity.getCorpid() + "_" + entity.getId());
        insertDTO.setSource(BeanUtil.convertBean2Map(entity, false, true));
        insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_CALL_CENTER_CALL_LOG);
        paasEsModel.update(insertDTO);
        return id;
    }

    @Override
    public void save(CallCenterCallLogEntity entity) throws XbbException{
        if (entity.getId() == null){
            insert(entity);
        }else {
            update(entity);
        }
    }

    /**
     * 获取符合查询条件的记录
     *
     * @param param 查询条件
     * @return 符合查询条件的记录
     */
    @Override
    public List<CallCenterCallLogEntity> findEntitys(Map<String, Object> param) {
        return callCenterCallLogDao.findEntitys(param);
    }

    /**
     * 获取符合查询条件的记录总数
     *
     * @param param 查询条件
     * @return 符合查询条件的记录总数
     */
    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return callCenterCallLogDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    /**
     * 物理删除
     * 直接删除这条记录
     *
     * @param key    主键
     * @param corpid 公司id
     * @return 删除数据库的行
     * @author youli.chen
     */
    @Override
    public Integer physicalDeleteByKey(Long key, String corpid) {
        return callCenterCallLogDao.physicalDeleteByKey(key, corpid);
    }

    /**
     * 新增一条阿里云呼通话记录
     * 此本新增只是在数据库先插入记录，具体内容待通话结束后保存
     *
     * @param callCenterCallLog 通话记录数据
     * @return ID
     */
    @Override
    public Integer aliyunCallCenterInsert(CallCenterCallLogEntity callCenterCallLog) throws XbbException {
        Integer id = callCenterCallLogDao.aliyunCallCenterInsert(callCenterCallLog);
        // 插入ES
        InsertDTO insertDTO = new InsertDTO();
        insertDTO.setEsId(callCenterCallLog.getCorpid() + "_" + callCenterCallLog.getId());
        insertDTO.setSource(BeanUtil.convertBean2Map(callCenterCallLog, false, true));
        insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_CALL_CENTER_CALL_LOG);
        paasEsModel.insert(insertDTO);
        return id;
    }

    @Override
    public Integer updateAliyunCallCenter(CallCenterCallLogEntity callLogEntity) throws XbbException {
        Integer integer = callCenterCallLogDao.updateAliyunCallCenter(callLogEntity);
        InsertDTO insertDTO = new InsertDTO();
        insertDTO.setEsId(callLogEntity.getCorpid() + "_" + callLogEntity.getId());
        insertDTO.setSource(BeanUtil.convertBean2Map(callLogEntity, false, true));
        insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_CALL_CENTER_CALL_LOG);
        paasEsModel.update(insertDTO);
        return integer;
    }

    @Override
    public Integer batchUpdateAliyunCallCenter(String corpid, List<CallCenterCallLogEntity> entityList) throws XbbException {
        if (CollectionUtils.isEmpty(entityList)) {
            return 0;
        }
        Integer integer = callCenterCallLogDao.batchUpdateAliyunCallCenter(corpid, entityList);
        List<InsertDTO> updateList = new ArrayList<>(entityList.size());
        for (CallCenterCallLogEntity entity : entityList) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(entity.getCorpid() + "_" + entity.getId());
            insertDTO.setSource(BeanUtil.convertBean2Map(entity, false, true));
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_CALL_CENTER_CALL_LOG);
            updateList.add(insertDTO);
        }
        paasEsModel.updateBatch(updateList);
        return integer;
    }

    /**
     * 插入
     *
     * @param entity 实体
     * @return 是否成功
     * @throws XbbException 数据库操作异常
     */
    @Override
    public Integer addYunCall(CallCenterCallLogEntity entity) throws XbbException {
        entity.setUpdateTime(DateUtil.getInt());
        Integer n = callCenterCallLogDao.insertYunCall(entity);

        InsertDTO insertDTO = new InsertDTO();
        insertDTO.setEsId(entity.getCorpid() + "_" + entity.getId());
        insertDTO.setSource(BeanUtil.convertBean2Map(entity, false, true));
        insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_CALL_CENTER_CALL_LOG);
        paasEsModel.insert(insertDTO);
        return n;
    }

    /**
     * 插入
     *
     * @param entity 实体
     * @return 是否成功
     * @throws XbbException 数据库操作异常
     */
    @Override
    public Integer updateYunCall(CallCenterCallLogEntity entity) throws XbbException {
        Integer integer = callCenterCallLogDao.updateYunCall(entity);
        InsertDTO insertDTO = new InsertDTO();
        insertDTO.setEsId(entity.getCorpid() + "_" + entity.getId());
        insertDTO.setSource(BeanUtil.convertBean2Map(entity, false, true));
        insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_CALL_CENTER_CALL_LOG);
        paasEsModel.update(insertDTO);
        return integer;
    }

    /**
     * 批量新增阿里云呼通话记录
     * 此本新增只是在数据库先插入记录，具体内容待通话结束后保存
     *
     * @param callLogList 通话记录数据
     * @return 影响行数
     */
    @Override
    public Integer aliyunCallBatchInsert(List<CallCenterCallLogEntity> callLogList) throws XbbException {
        if (CollectionUtils.isEmpty(callLogList)) {
            return 0;
        }
        Integer integer = callCenterCallLogDao.aliyunCallBatchInsert(callLogList);
        List<InsertDTO> insertList = new ArrayList<>();
        for (CallCenterCallLogEntity entity : callLogList) {
            // 插入ES
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(entity.getCorpid() + "_" + entity.getId());
            insertDTO.setSource(BeanUtil.convertBean2Map(entity, false, true));
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_CALL_CENTER_CALL_LOG);
            insertList.add(insertDTO);
        }
        paasEsModel.insertBatch(insertList);
        return integer;
    }

}
